<?php
class CuonsachesController extends AppController {

	var $name = 'Cuonsaches';

	function index() {
		$this->Cuonsach->recursive = 0;
		$this->set('cuonsaches', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid cuonsach', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('cuonsach', $this->Cuonsach->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Cuonsach->create();
			if ($this->Cuonsach->save($this->data)) {
				$this->Session->setFlash(__('Thông tin đã lưu', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('Thông tin không thể lưu.', true));
			}
		}
		$dausaches = $this->Cuonsach->Dausach->find('list');
		$this->set(compact('dausaches'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid cuonsach', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Cuonsach->save($this->data)) {
				$this->Session->setFlash(__('The cuonsach has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The cuonsach could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Cuonsach->read(null, $id);
		}
		$dausaches = $this->Cuonsach->Dausach->find('list');
		$this->set(compact('dausaches'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for cuonsach', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Cuonsach->delete($id)) {
			$this->Session->setFlash(__('Cuonsach deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Cuonsach was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}

	function dscuonsach($id)
    {
    	if(!$id)
    	{
    		$this->Session->setFlash(__('Đầu sách không tồn tại', true));
			$this->redirect(array('controller'=>'dausaches','action'=>'timkiem'));
    	}
    
      $dausach=$this->Cuonsach->Dausach->find('first',array('recursive'=>2,'conditions' => array('Dausach.id'=>$id)));
      
      $cuonsaches=$this->Cuonsach->find('all', array('recursive'=>2,'conditions' => array('dausach_id' => $id)));
      $cuonsach_id=$this->Cuonsach->find('list', array('conditions' => array('dausach_id' => $id),'fields'=>array('id')));
      $giaodiches=$this->Cuonsach->Giaodichmuontra->find('list',array('conditions'=>array('cuonsach_id'=>$cuonsach_id,'ngaytra'=>null),'fields'=>array('cuonsach_id','cuonsach_id'))) ;
      $this->set(compact('dausach','cuonsaches','giaodiches')); 
    }
}
